using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
using yW5Lib.My;

namespace yW5Lib
{
	[StandardModule]
	internal sealed class modSaveLog
	{
		public static void WriteWebFile(clsProject cProject)
		{
			//Discarded unreachable code: IL_0263, IL_027a, IL_0285, IL_0295, IL_032b, IL_032d, IL_0341, IL_0363
			int num = default(int);
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				string TextString = "";
				num2 = 3;
				string logsFolder = cProject.LogsFolder;
				num2 = 4;
				string text = Path.Combine(MyProject.Application.Info.DirectoryPath, "WebTemplate");
				num2 = 5;
				clsShared.cFile.MkMissingDir(logsFolder);
				num2 = 6;
				string filename = Path.Combine(logsFolder, "index.html");
				num2 = 7;
				string text2 = Path.Combine(logsFolder, "WebTemplate.txt");
				num2 = 8;
				if (clsShared.cFile.FolderExists(text))
				{
					num2 = 9;
					if (!clsShared.cFile.FileExists(text2))
					{
						num2 = 10;
						clsShared.cFile.CopyFile(Path.Combine(text, "WebTemplate.txt"), text2);
					}
					num2 = 11;
					string text3 = Path.Combine(logsFolder, "logo.png");
					num2 = 12;
					if (!clsShared.cFile.FileExists(text3))
					{
						num2 = 13;
						clsShared.cFile.CopyFile(Path.Combine(text, "Logo.png"), text3);
					}
					num2 = 14;
					string text4 = Path.Combine(logsFolder, "style.css");
					num2 = 15;
					if (!clsShared.cFile.FileExists(text4))
					{
						num2 = 16;
						clsShared.cFile.CopyFile(Path.Combine(text, "style.css"), text4);
					}
					num2 = 17;
					string text5 = Path.Combine(logsFolder, "copyto.bat");
					num2 = 18;
					if (!clsShared.cFile.FileExists(text5))
					{
						num2 = 19;
						clsShared.cFile.CopyFile(Path.Combine(text, "copyto.bat"), text5);
					}
					num2 = 20;
					clsShared.cFile.LoadTextFile(text2, ref TextString);
					num2 = 21;
					if (Operators.CompareString(cProject.FileWithPath, "", TextCompare: false) != 0)
					{
						num2 = 22;
						TextString = Strings.Replace(TextString, "$PROJECTTITLE", cProject.Title);
						num2 = 23;
						TextString = Strings.Replace(TextString, "$WORDCOUNT", Conversions.ToString(cProject.Wordcount()));
						num2 = 24;
						TextString = Strings.Replace(TextString, "$LASTMOD", Strings.Format(DateAndTime.Now.Date, "YYYY-MM-DD"));
						num2 = 25;
						if (Operators.CompareString(Strings.Trim(TextString), "", TextCompare: false) != 0)
						{
							num2 = 26;
							clsShared.cFile.WriteTextFile(filename, TextString);
							num2 = 27;
							if (clsShared.cFile.FileExists(text5))
							{
								num2 = 28;
								clsShared.cFile.RunFileHidden(text5, logsFolder);
							}
						}
					}
				}
				else
				{
					num2 = 30;
					clsShared.cLog.AddAction("Could not create a copy of the webtemplate files ... the source folder is missing.");
				}
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_0361: Could not find block for branch target IL_032d*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		public static void WriteProgressLog(clsProject cProject)
		{
			try
			{
				string logsFolder = cProject.LogsFolder;
				clsShared.cFile.MkMissingDir(logsFolder);
				StringBuilder stringBuilder = new StringBuilder();
				if (Operators.CompareString(cProject.FileWithPath, "", TextCompare: false) != 0)
				{
					string text = Path.Combine(logsFolder, Strings.Format(DateAndTime.Now, "yyyy-MM-dd") + " - " + clsShared.cFile.Fileroot(clsShared.cFile.FileOnly(cProject.FileWithPath)) + ".txt");
					stringBuilder.AppendLine("Word count summary for " + cProject.Title + " by " + cProject.AuthorName);
					stringBuilder.AppendLine("");
					stringBuilder.AppendLine("");
					stringBuilder.AppendLine("Chapter,Words");
					IEnumerator enumerator = default(IEnumerator);
					try
					{
						enumerator = cProject.Chapters.GetEnumerator();
						while (enumerator.MoveNext())
						{
							clsChapter clsChapter = (clsChapter)enumerator.Current;
							stringBuilder.AppendLine(clsChapter.Title + "," + Conversions.ToString(cProject.get_ChapterWordcount(clsChapter.ID, WithUnused: true)));
						}
					}
					finally
					{
						if (enumerator is IDisposable)
						{
							(enumerator as IDisposable).Dispose();
						}
					}
					stringBuilder.AppendLine("");
					stringBuilder.AppendLine("Total word count," + Conversions.ToString(cProject.Wordcount()) + ",Date," + Strings.Format(DateAndTime.Now, "yyyy-MM-dd"));
					stringBuilder.AppendLine("Total with unused," + Conversions.ToString(cProject.WordcountWithUnused()) + ",Date," + Strings.Format(DateAndTime.Now, "yyyy-MM-dd"));
					stringBuilder.AppendLine("");
					stringBuilder.AppendLine("Generated by " + MyProject.Application.Info.Title + " (c) " + Strings.Format(DateAndTime.Now, "yyyy") + " Spacejock Software");
					if (clsShared.cFile.WriteTextFile(text, stringBuilder.ToString()))
					{
						clsShared.cLog.AddAction("Progress log written to " + text);
					}
					else
					{
						clsShared.cLog.AddAction("Unable to write progress log " + text);
					}
				}
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}
	}
}
